import { fetchEventSource } from "@microsoft/fetch-event-source"

import type { ChatMessageType, ChatQueryType } from "~type/chatType"
import type { TokenType } from "~type/tokenType"
import { request } from "~util/request"

export function getBaiduToken({
  grantType = "client_credentials",
  apiKey,
  secretKey
}: TokenType) {
  return request.get("https://aip.baidubce.com/oauth/2.0/token", {
    headers: {
      "Content-Type": "application/json"
    },
    searchParams: {
      grant_type: grantType,
      client_id: apiKey,
      client_secret: secretKey
    }
  })
}
export function chatToBaiduAI(data: ChatQueryType) {
  const ctrl = new AbortController()

  return request.post(
    `https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=${data.accessToken}`,
    {
      json: data,
      signal: ctrl.signal
    }
  )
}
